<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item label="寝室id" prop="id">
        <el-input v-model="dataForm.id" placeholder="寝室id"></el-input>
      </el-form-item>
      <el-form-item label="可住人数" prop="maxLiveNum">
        <el-input v-model="dataForm.maxLiveNum" placeholder="可住人数"></el-input>
      </el-form-item>
      <el-form-item label="剩余空位" prop="remainNum">
        <el-input v-model="dataForm.remainNum" placeholder="剩余空位"></el-input>
      </el-form-item>
      <el-form-item label="住宿费用" prop="cost">
        <el-input v-model="dataForm.cost" placeholder="住宿费用"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="电话"></el-input>
      </el-form-item>
      <el-form-item label="公寓楼号" prop="flatNo">
        <el-input v-model="dataForm.flatNo" placeholder="公寓楼号"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      // 数据
      visible: false,
      dataForm: {
        id: 0,
        maxLiveNum: 0,
        remainNum: 0,
        cost: 0,
        phone: "",
        flatNo: 0,
      },
      dataRule: {
        flatNo: [
          { required: true, message: "公寓楼号不能为空", trigger: "blur" },
        ],
        maxLiveNum: [
          { required: true, message: "可住人数不能为空", trigger: "blur" },
        ],
        cost: [
          { required: true, message: "住宿费用不能为空", trigger: "blur" },
        ],
        phone: [
          {
            required: true,
            message: "电话不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  components: {
    // 组件注册
  },
  methods: {
    //表单提交
    dataFormSubmit() {
      console.log("submit");
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/apartment/dormitoryinfo/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              flatNo: this.dataForm.flatNo,
              maxLiveNum: this.dataForm.maxLiveNum,
              remainNum: this.dataForm.remainNum,
              cost: this.dataForm.cost,
              phone: this.dataForm.phone,
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    //组件初始化方法
    init(myid) {
      this.dataForm.id = myid || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/apartment/dormitoryinfo/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.maxLiveNum = data.dormitoryInfo.maxLiveNum;
              this.dataForm.remainNum = data.dormitoryInfo.remainNum;
              this.dataForm.cost = data.dormitoryInfo.cost;
              this.dataForm.phone = data.dormitoryInfo.phone;
              this.dataForm.flatNo = data.dormitoryInfo.flatNo;
            }
          });
        }
      });
    },
  },
};
</script>